<?php

	class EventItemCommentBusiness {
		
		public function selectEventItemCommentByEventItemId($eventItemId) {
			$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
			
			$dataBaseConnection->connect();
			
			$eventItemCommentDataAccess = DataAccessFactory::factory('EventItemCommentDataAccessMySQL');
			
			$result = $eventItemCommentDataAccess->selectEventItemCommentByEventItemId($eventItemId, $dataBaseConnection);
			
			$dataBaseConnection->disconnect();
			
			return $result;
		}
		
		public function saveComment($comment, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemCommentDataAccess = DataAccessFactory::factory('EventItemCommentDataAccessMySQL');
			
			$result = $eventItemCommentDataAccess->saveComment($comment, $dataBaseConnection);
			
			if ($connectionOpened) {
				if ($result == -1) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}
			
			return $result;
		}
		
		
		public function deleteEventItemCommentByEventItemAttendant($eventItem, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			$eventItemComment = new EventItemComment();
			$eventItemComment->setEventItemId($eventItem->getEventItemId());
			$eventItemComment->setCommentUser($eventItem->getEventItemUser());
			
			$result = $this->deleteEventItemComment($eventItemComment, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
		
		public function deleteEventItemComment($eventItemComment, $dataBaseConnection = null) {
			$connectionOpened = false;
			if ($dataBaseConnection == null) {
				$connectionOpened = true;
				$dataBaseConnection = DataAccessFactory::factory('DataAccessMySQL');
				$dataBaseConnection->connect();
				$dataBaseConnection->beginTransaction();
			}
			
			$eventItemCommentDataAccess = DataAccessFactory::factory('EventItemCommentDataAccessMySQL');
			$result = $eventItemCommentDataAccess->deleteEventItemComment($eventItemComment, $dataBaseConnection);
			
			if ($connectionOpened) {
				if (!$result) {
					$dataBaseConnection->rollback();
				} else {
					$dataBaseConnection->commit();
				}
				$dataBaseConnection->disconnect();
			}			
			return $result;
		}
		
		
	}

?>